# Definition for singly-linked list.
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None


class Solution:
    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        t = head = ListNode(-1)

        inc = 0
        while l1 is not None or l2 is not None:
            t0 = l1.val if l1 is not None else 0
            t1 = l2.val if l2 is not None else 0

            sum = t0 + t1 + inc
            
            inc, sum = sum // 10, sum % 10
            t.next = ListNode(sum)

            l1 = l1.next if l1 is not None else None
            l2 = l2.next if l2 is not None else None
            t = t.next

        if inc > 0:
            t.next = ListNode(inc)

        return head.next


def build_list_num(*args):
    t = head = ListNode(-1)
    for i in args:
        t.next = ListNode(i)
        t = t.next
    return head.next


def print_list_num(list_num):
    while(list_num is not None):
        print(list_num.val, end="  ")
        list_num = list_num.next
    print()


if __name__ == "__main__":
    num0 = build_list_num(2,4,3)
    num1 = build_list_num(5,6,7,9)

    print("num0",end=":")
    print_list_num(num0)

    print("num1",end=":")
    print_list_num(num1)

    sum = Solution().addTwoNumbers(num0, num1)
    print("sum ",end=":")
    print_list_num(sum)
